package com.bkpowernode.javaWeb.servlet;

import jakarta.servlet.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

public class AServlet extends GenericServlet {
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();

        ServletContext application=this.getServletContext();
        out.print("ServletContext对象是："+application+"</br>");

        //获取上下文初始化参数信息
        Enumeration<String> names=application.getInitParameterNames();
        while (names.hasMoreElements()){
            String name=names.nextElement();
            String value=application.getInitParameter(name);
            out.print(name+"="+value+"</br>");
        }

        //获取Context path（获取上下文应用的根）
        String path=application.getContextPath();
        out.print(path+"</br>");
        ///servlet04

        //获取文件绝对路径 '/'代表的是web的根
        //        String realPath=application.getRealPath("/index.html"); 可以
//        String realPath=application.getRealPath("index.html"); //不加/也可以 默认也是从web根路径下开始找
//        out.print(realPath+"</br>");

        String realPath=application.getRealPath("/common/common.html"); //不加/也可以 默认也是从web根路径下开始找
        out.print(realPath+"</br>");

        //存
        User user=new User("jack","123");
        application.setAttribute("userObj",user);

        //取
        Object userObj=application.getAttribute("userObj");
        out.print(userObj+"</br>");

    }
}
